package com.eclipse.organigram.views.ui;

import java.io.PrintWriter;
import java.io.StringWriter;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

public class ErrorComposite extends Composite {

	private Text text;
	
	public ErrorComposite(Composite parent, Exception e) {
		this(parent, getErrorText(e));
	}

	private ErrorComposite(Composite parent, String errorMessage) {
		super(parent, SWT.NONE);
		setLayout(new GridLayout(1, false));
		
		Label lblNewLabel = new Label(this, SWT.NONE);
		lblNewLabel.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		lblNewLabel.setFont(SWTResourceManager.getFont("Tahoma", 13, SWT.BOLD));
		lblNewLabel.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false, 1, 1));
		lblNewLabel.setText("ERROR: You organigram is not valid. Fix it and try again.");
		
		text = new Text(this, SWT.BORDER | SWT.V_SCROLL | SWT.MULTI);
		text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		text.setText(errorMessage);
	}

	private static String getErrorText(Exception e) {
		StringWriter sw = new StringWriter();
		PrintWriter pw = new PrintWriter(sw);
		if (e.getCause() != null) {
			e.getCause().printStackTrace(pw);
		} else {
			e.printStackTrace(pw);
		}
		return sw.toString();
	}

}
